package util;

import java.util.concurrent.*;

/**
 * @author Administrator
 * @date 2024-10-29 19:11
 */
public class MyThreadPool {
    //饿韩式
    private static final MyThreadPool MY_THREAD_POOL =new MyThreadPool();

   private static  final ThreadPoolExecutor POOL_EXECUTOR;

   private  static  final ScheduledThreadPoolExecutor SCHEDULED_THREAD_POOL_EXECUTOR;

   static {
       //静态代码块
//       POOL_EXECUTOR=new ScheduledThreadPoolExecutor(10,100,10,TimeUnit.SECONDS,new ArrayList<>(100),Executors.defaultThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy());
       POOL_EXECUTOR=new ThreadPoolExecutor(10,10,100,TimeUnit.SECONDS,new ArrayBlockingQueue<>(100),Executors.defaultThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy());

       SCHEDULED_THREAD_POOL_EXECUTOR=new ScheduledThreadPoolExecutor(10,new ThreadPoolExecutor.CallerRunsPolicy());
   }
   public static MyThreadPool getInstance(){

       return  MY_THREAD_POOL;
   }
   public static void execute(Runnable runnable){
       POOL_EXECUTOR.execute(runnable);
   }
   //延迟加载
    public static void delay(Runnable runnable,long time,TimeUnit timeUnit){
       SCHEDULED_THREAD_POOL_EXECUTOR.schedule(runnable,time,timeUnit);
    }
    //异步执行
    public<V>  void  task(Callable<V> callable){
       POOL_EXECUTOR.submit(callable);
    }
    public static void  release (){
       POOL_EXECUTOR.shutdown();
    }
    public static void  releaseNow (){
        POOL_EXECUTOR.shutdownNow();
    }
    public static void shutdown(){
       SCHEDULED_THREAD_POOL_EXECUTOR.shutdown();
    }
    public static void  shutdownNow(){
       SCHEDULED_THREAD_POOL_EXECUTOR.shutdownNow();
    }




}
